package com.learn.bus;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>项目名称: design-pattern </p>
 *
 * <p>文件名称: DataBus </p>
 *
 * This implementation uses a Singleton.
 */
public class DataBus {

    private static final DataBus INSTANCE = new DataBus();

    private final Set<Member> members = new HashSet<>();

    public static DataBus getInstance(){
        return INSTANCE;
    }

    public void subscribe(final Member member){
        this.members.add(member);
    }

    public void unsubscribe(final Member member){
        this.members.remove(member);
    }

    public void publish(final Data data){
        data.setDataBus(this);
        members.forEach(member -> member.accept(data));
    }

}
